import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
from python_ai.common.xcommon import *
import datetime

image = cv.imread('../../../../../large_data/pic/football.jpg',0)
img = image.copy()
rows, cols = img.shape
print('img', img.shape)

template = cv.imread('../../../../../large_data/CV2/lesson/Day07/football_ball.jpg',0)
h, w = template.shape
print('template', template.shape)

img_norm = img/255
template_norm = template/255

img_norm = img_norm - np.mean(img_norm)
template_norm = template_norm - np.mean(template_norm)

dt1 = datetime.datetime.now()
print('1 ...')
res1 = convolution_numpy_acc(img_norm, template_norm)
print('1 done')
dt2 = datetime.datetime.now()
print('numpy acc: ', dt2 - dt1)
print('2 ...')
res2 = convolution_numpy(img_norm, template_norm)
print('2 done')
dt3 = datetime.datetime.now()
print('numpy: ', dt3 - dt2)
print('3 ...')
res3 = convolution(img_norm, template_norm)
print('3 done')
dt4 = datetime.datetime.now()
print('python: ', dt4 - dt3)

print('res1 res2 consistent: ', np.allclose(res1, res2))
print('res2 res3 consistent: ', np.allclose(res2, res3))

res = res1
idx = res.argmax()
cx, cy = np.unravel_index(idx, res.shape)
print('center', cx, cy)
